(function($){
	var pager = {
			init: function(obj,page){
				return (function(){
					page['mark'] = 0;//标记列表是否是初次加载
					pager.excuteFn(obj,page.pageIndex,page);
				})();
			},
			fillhtml: function(obj,page){
				return (function(){
					obj.empty();
					if(page.totalPage > 0){
						obj.append('<span class="pageSpan"><a class="firstPage"  >首页</a></span>');
						obj.append('<span class="pageSpan"><a class="beforePage"  >上一页</a></span>');
						if(page.totalPage <= page.showIndex){
							for(var i = 1;i <= page.totalPage;i++){
								obj.append('<span class="pageNum"><a class="numPage" >'+i+'</a></span>');
							}
						}else{
							var start = 0;
							var end = 0;
							var checkNum = Math.floor(page.showIndex/2);
							if(page.pageIndex - checkNum > 1){
								obj.append("<span style='float:left;text-align:center;'>...</span>");
							}
							if((page.pageIndex - checkNum) <= 0){
								start = 1;
								end = page.showIndex;
							}
							else if((page.pageIndex + checkNum) >= page.totalPage){
								start = page.totalPage - page.showIndex + 1;
								end = page.totalPage;
							}else if((page.pageIndex - checkNum) > 0 && (page.pageIndex + checkNum) < page.totalPage){
								start = page.pageIndex - checkNum;
								end = page.pageIndex + checkNum;
							}
							if((end - start + 1) > page.showIndex ){
								start = start + 1;
							}
							
							for(var i = start;i <= end ;i++){
								obj.append('<span class="pageNum"><a class="numPage" target="'+i+'" >'+i+'</a></span>');
							}
							if(page.pageIndex + checkNum < page.totalPage){
								obj.append("<span style='float:left;text-align:center;'>...</span>");
							}
						}
						obj.append('<span class="pageSpan"><a class="nextPage"  >下一页</a></span>');
						obj.append('<span class="pageSpan"><a class="lastPage" >尾页</a></span>');
						
					}
				})();
			},
			prop: function(obj,key,val){
				return (function(){
					if(typeof val === "undefined"){
						delete obj[key];
					}else{
						obj[key] = val;
					}
				})();
			},
			createTable: function(page){
				return (function(){
					var data = page.data;
					$("#"+page.tbodyId).html('');
					for(var i = 0;i < page.data.length;i++){
						var tr = "<tr>";
						$("#"+page.theadId).find("th").each(function(index){
							var cname = $(this).attr("cname");
							if(cname != undefined && cname != ''){
								var td = "<td>"+page.data[i][cname]+"</td>";
								tr += td;
							}
							
						});
						tr += "</tr>";
						$("#"+page.tbodyId).append(tr);
					}
					pager.dealNumberAndBox(page);
				})();
				
			},
			dealNumberAndBox:function(page){
				if(page.showNum == true){
					if(page.mark == '0'){
						$("#"+page.theadId).find("tr").eq(0).prepend("<th>编号</th>");	
					}
					$("#"+page.tbodyId).find("tr").each(function(index){
						$(this).prepend("<td>"+(parseInt(index)+1)+"</td>");
					});
				}
				if(page.showCheckBox == true){
					if(page.mark == '0'){
						$("#"+page.theadId).find("tr").eq(0).prepend("<th style='width:20px;'><input id='"+page.theadId+"_checkbox' type='checkbox'></th>");
						$("#"+page.theadId).find("input[type=checkbox]").eq(0).on("click",function(){
							var mark = $(this).prop("checked");
							$("#"+page.tbodyId).find("tr").each(function(index){
								$(this).find("input[type=checkbox]").eq(0).prop("checked",mark);
								if(mark == true){
									$(this).unbind("mouseover");
									$(this).unbind("mouseout");
									$(this).addClass("tr_focus").removeClass("tr_mouse_over");
								}else{
									$(this).removeClass("tr_focus").removeClass("tr_mouse_over");
									pager.bindTrMouse(this);
								}
							});
						});
					}
				}
					$("#"+page.tbodyId).find("tr").each(function(index){
						pager.bindTrMouse(this);
						if(page.showCheckBox == true){
							$(this).prepend("<td><input id='"+page.tbodyId+"_"+index+"_checkbox' type='checkbox'></td>");
							
							$(this).on("click",function(){
								var boxMark = true;
								$(this).addClass("tr_focus").removeClass("tr_mouse_over");
								$(this).unbind("mouseover");
								$(this).unbind("mouseout");
								
								var trCheckBox = $(this).find("input[type=checkbox]").eq(0);
								if(trCheckBox.prop("checked") == true){
									trCheckBox.prop("checked",false);
									pager.bindTrMouse(this);
									$(this).removeClass("tr_focus").removeClass("tr_mouse_over");
									$("#"+page.theadId).find("input[type=checkbox]").eq(0).prop("checked",false);
								}else{
									trCheckBox.prop("checked",true);
									$("#"+page.tbodyId).find("tr").each(function(index){
										var trCheckBox = $(this).find("input[type=checkbox]").eq(0);
										if(trCheckBox.prop("checked") == false){
											boxMark = false;
										}
									});
									$("#"+page.theadId).find("input[type=checkbox]").eq(0).prop("checked",boxMark);
								}
							});
							
						}
						
					});
				page.mark = '1';
			},
			bindTrMouse:function(obj){
				$(obj).on("mouseover",function(){
					$(this).addClass("tr_mouse_over");
				});
				$(obj).on("mouseout",function(){
					$(this).removeClass("tr_mouse_over");
				});
			},
			getData:function(page){
				return (function(){
					$.ajax({
						type: "POST",
						data: page.params,
						url: page.url,
						async: false,
						success: function(data){
							var result;
							if(typeof(data) == "string"){
								result = jQuery.parseJSON(data);
							}else{
								result = data;
							}
							/***处理页码数据**/
							if(!("totalPage" in page)){
								page.totalPage = result.conut;
							}
							page.data = result.data;
							console.log(page);
						}
					});
				})();
			},
			createOptions:function(pageIndx,totalPage,options){
				return (function(){
					var page = $.extend(options,{
						pageIndex: pageIndx || 1,
						totalPage: totalPage || 10});
					return page;
				})();
			},
			excuteFn:function(obj,pageIndex,page){
				return (function(){
					var pageHtml = $("#"+page.pageIndexId);
					page.pageIndex = pageIndex == null ? 1 : pageIndex;
					pager.getData(page);
					pager.createTable(page);
					pager.fillhtml(pageHtml,page);
					pager.bindEvent(pageHtml,page);
					pager.changeFocus(pageHtml,page.pageIndex);
				})();
			},
			changeFocus:function(obj,pageIndex){
				return (function(){
					var $ids = obj.find(".numPage");
					$ids.each(function(index){
						$(this).parent().removeClass("focus");
						if(parseInt($(this).attr("target")) == pageIndex){
							$(this).parent().addClass("focus");
						}
					});
				})();
			},
			bindEvent:function(obj,page){
				return (function(){
					var $ids = obj.find("a");
					$ids.each(function(index){
						$(this).off("click");
						var className = $(this).attr("class");
						if(className == 'firstPage'){
							$(this).parent().on("click",function(){
								pager.excuteFn(obj,1,page);
							});
						}
						if(className == 'beforePage'){
							$(this).parent().on("click",function(){
								var index = 1;
								if(page.pageIndex > 1){
									index = page.pageIndex - 1;
								}else{
									index = 1;
								}
								index = Number(index);
								pager.excuteFn(obj,index,page);
							});
						}
						if(className == 'numPage'){
							$(this).parent().on("click",function(){
								var index = $(this).find('a').attr("target");
								index = Number(index);
								pager.excuteFn(obj,index,page);
							});
						}
						if(className == 'nextPage'){
							$(this).parent().on("click",function(){
								var index = 1;
								if(page.pageIndex < page.totalPage){
									index = page.pageIndex + 1;
								}else{
									index = page.totalPage;
								}
								index = Number(index);
								pager.excuteFn(obj,index,page);
							});
						}
						if(className == 'lastPage'){
							$(this).parent().on("click",function(){
								pager.excuteFn(obj,page.totalPage,page);
							});
						}
						
					});
				})();
			}
	}
	$.fn.extend({
		createPage:function(options){
			var page = $.extend({
				showIndex:5,
				url: null,
				params: null,
				theadId:"",
				tbodyId: "",
				showNum:true,
				showCheckBox:true,
				backFn:function(p){
					
				}
			},options);
			pager.init(this,page);
		}
	});
})(jQuery);